package src;

import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;

public class AddDialog extends JDialog {
    protected Connection connection;
    protected JPanel inputPanel;
    protected JButton confirmButton, cancelButton;
    protected Map<String, JTextField> textFields;
    protected Map<String, JComboBox<String>> comboBoxes;

    public AddDialog(Window parent, String title, Connection connection) {
        super(parent, title, ModalityType.APPLICATION_MODAL);
        this.connection = connection;
        this.textFields = new HashMap<>();
        this.comboBoxes = new HashMap<>();
        
        setSize(600, 700);
        setLocationRelativeTo(parent);
        getContentPane().setBackground(StyleConstants.BACKGROUND_COLOR);

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBackground(StyleConstants.BACKGROUND_COLOR);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));

        // 创建标题面板
        JPanel titlePanel = new JPanel();
        titlePanel.setBackground(StyleConstants.THEME_COLOR);
        JLabel titleLabel = new JLabel(title, JLabel.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titleLabel.setForeground(Color.WHITE);
        titlePanel.add(titleLabel);
        mainPanel.add(titlePanel, BorderLayout.NORTH);

        // 创建输入面板
        inputPanel = new JPanel();
        inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
        inputPanel.setBackground(StyleConstants.BACKGROUND_COLOR);
        inputPanel.setBorder(BorderFactory.createEmptyBorder(20, 30, 20, 30));
        mainPanel.add(inputPanel, BorderLayout.CENTER);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 30, 15));
        buttonPanel.setBackground(StyleConstants.BACKGROUND_COLOR);

        confirmButton = createStyledButton("确认");
        cancelButton = createStyledButton("取消");
        cancelButton.addActionListener(e -> dispose());

        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(mainPanel);
    }

    protected void addInputField(String labelText, JTextField textField) {
        JPanel fieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        fieldPanel.setBackground(StyleConstants.BACKGROUND_COLOR);
        
        JLabel label = new JLabel(labelText);
        label.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        label.setPreferredSize(new Dimension(120, 35));
        
        textField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        textField.setPreferredSize(new Dimension(300, 35));
        
        fieldPanel.add(label);
        fieldPanel.add(textField);
        inputPanel.add(fieldPanel);
        inputPanel.add(Box.createVerticalStrut(15));
        
        textFields.put(labelText, textField);
    }

    protected void addComboBox(String labelText, JComboBox<String> comboBox) {
        JPanel fieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        fieldPanel.setBackground(StyleConstants.BACKGROUND_COLOR);
        
        JLabel label = new JLabel(labelText);
        label.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        label.setPreferredSize(new Dimension(120, 35));
        
        comboBox.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        comboBox.setPreferredSize(new Dimension(300, 35));
        
        fieldPanel.add(label);
        fieldPanel.add(comboBox);
        inputPanel.add(fieldPanel);
        inputPanel.add(Box.createVerticalStrut(15));
        
        comboBoxes.put(labelText, comboBox);
    }

    protected JButton createStyledButton(String text) {
        JButton button = new JButton(text);
        button.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        button.setBackground(StyleConstants.BUTTON_COLOR);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));

        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(StyleConstants.BUTTON_COLOR.darker());
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(StyleConstants.BUTTON_COLOR);
            }
        });

        return button;
    }
} 